//
//  CYDataManager.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/11.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYCache.h"
#import "CYIPAddress.h"
#import "CYTime.h"
@implementation CYCache
+ (NSString *)getDocumentPath{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
}

+ (NSString *)getCachesPath{
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
}

+ (NSString *)getPreferencesPath{
    return [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"Preferences"];
}

+ (NSString *)getTmpPath{
    return NSTemporaryDirectory();
}

+ (NSString *)getIP{
    return [CYIPAddress getIPAddress];
}

//存储
+ (void)saveData:(id)data forKey:(NSString *)key synchronize:(BOOL)isSynchronize{
    if (!data || !key)
        return;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];//NSUserDefaults数据保存在沙盒路径Preferences下
    [defaults setObject:data forKey:key];
    if (isSynchronize)
        [defaults synchronize];
}

//读取
+ (id)readDataWithKey:(NSString *)key{
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}

//删除
+ (void)deleteDataWithKey:(NSString *)key{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}

//存储到caches
+ (BOOL)cache:(id)data plist:(NSString*)plist{
    if (!data || !plist)
        return NO;
    NSString *new_plist = ([plist containsString:@".plist"])?[NSString stringWithFormat:@"/%@",plist]:[NSString stringWithFormat:@"/%@.plist",plist];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *path = [[CYCache getCachesPath] stringByAppendingString:new_plist];
        [NSKeyedArchiver archiveRootObject:data toFile:path];
    });
    return YES;
}

//从caches读取数据
+ (id)getCache:(NSString *)plist{
    if (!plist)
        return nil;
    NSString *new_plist = ([plist containsString:@".plist"])?[NSString stringWithFormat:@"/%@",plist]:[NSString stringWithFormat:@"/%@.plist",plist];
    NSString *path = [[CYCache getCachesPath] stringByAppendingString:new_plist];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

//转换成时间
+ (void)time:(NSUInteger)number block:(void(^)(NSString * time))block{
    
    //    [CYTime countDown:number time:^(NSString *time) {
    //        block(time);
    //    }];
}
@end
